package utils
{
	import mx.core.UIComponent;
	
	import interfaces.IDispose;
	import interfaces.IReset;

	public class ContainerUtil
	{
		/**
		 * 获取父容器内某种类型的子控件集合
		 */
		public static function getChildren(ui:UIComponent, p_types:Array = null):Array
		{
			var arr:Array = [];
			var n:int = ui.numChildren;
			for(var i:int = 0;i < n;i++){
				var child:UIComponent = ui.getChildAt(i) as UIComponent;
				if(child){
					if(p_types){
						for each(var clz:* in p_types){
							if(child is clz){
								arr.push(child);
								break;
							}
						}
					}else{
						arr.push(child);
					}
					if(child.numChildren > 0)
						arr = arr.concat(getChildren(child, p_types));
				}
			}
			return arr;
		}
		
		/**
		 * 释放窗口内所有子控件占用的资源（包含父容器）
		 */
		public static function diposeAll(ui:UIComponent):void
		{
			var arr:Array = getChildren(ui, [IDispose]);
			arr.push(ui);
			for(var i:int = 0;i < arr.length;i++){
				var child:UIComponent = arr[i] as UIComponent;
				if(child is IDispose)
					IDispose(child).dispose();
			}
		}
		
		/**
		 * 重置控件
		 * @param p_ui
		 * 
		 */		
		public static function resetComponent(p_ui:UIComponent):void
		{
			var cmpts:Array = getChildren(p_ui, [IReset]);
			for(var i:int = 0;i < cmpts.length;i++){
				var ui:IReset = cmpts[i] as IReset;
				ui.reset();
			}
		}
	}
}